//
//  BaseAnchorViewController.swift
//  DouYuZB
//
//  Created by 中创 on 2020/4/24.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

let itemWidth:CGFloat = (kScreenWidth - 30) / 2
let itemHeight:CGFloat = itemWidth * 3 / 4
let itemId:String = "itemId"
let kItemPrettyId:String = "kItemPrettyId"
private let kItemMagin:CGFloat = 10.0
private let headerHeight:CGFloat = 40.0
private let headerId:String = "headerId"


class BaseAnchorViewController: BaseViewController {

    /// collectionview
    lazy var collectionView:UICollectionView = { [unowned self] in
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 0, left: kItemMagin, bottom: 0, right: kItemMagin)
        layout.headerReferenceSize = CGSize(width: kScreenWidth, height: headerHeight)
        let colFrame = CGRect(x: 0, y: 0, width: kScreenWidth, height: self.view.bounds.size.height)
        let collectionView = UICollectionView(frame: colFrame, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // 让宽高随着父试图变化而变化
        collectionView.register(UINib(nibName: "CollectionNormalCell", bundle: nil), forCellWithReuseIdentifier: itemId)
        collectionView.register(UINib(nibName: "CollectionPrettyCell", bundle: nil), forCellWithReuseIdentifier: kItemPrettyId)
        collectionView.register(UINib(nibName: "RecommendColHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerId)
        collectionView.backgroundColor = UIColor.white
        return collectionView
    }()
    var baseVM:BaseAnchorViewModel?
    override func viewDidLoad() {
        super.viewDidLoad()
        getData()
    }

}

// MARK: 遵守协议
extension BaseAnchorViewController:UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: itemId, for: indexPath) as! CollectionNormalCell
        if baseVM == nil {
            return cell
        }
        let section = indexPath.section
        let room_list = baseVM!.anchorGroups[section].room_list
        cell.room = room_list[indexPath.row]
        cell.backgroundColor = UIColor.white
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! RecommendColHeader
        header.anchorGroup = baseVM!.anchorGroups[indexPath.section]
        header.backgroundColor = UIColor.white
        return header
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if baseVM == nil {return 1}
        return baseVM!.anchorGroups.count
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if baseVM == nil {return 20}
        let rooms = baseVM!.anchorGroups[section].room_list.count
        return rooms
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: itemWidth, height: itemHeight)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let room_list = baseVM!.anchorGroups[indexPath.section].room_list
        let room = room_list[indexPath.row]
//        room.isVertical == 0 ? pushNormalRoom() : presentShowRoom()
    }
    
}
// MARK:action
extension BaseAnchorViewController{
    /// 跳转到普通房间
    private func pushNormalRoom(){
        navigationController?.pushViewController(RoomNormalViewController(), animated: true)
    }
    /// 跳转到秀场房间
    private func presentShowRoom(){
        present(RoomShowViewController(), animated: true, completion: nil)
    }
}
// MARK: 设置UI
extension BaseAnchorViewController{
    override func setupUI() {
        contentView = collectionView
        view.addSubview(collectionView)
        super.setupUI()
    }
    
}
// MARK: 网络数据
extension BaseAnchorViewController{
     @objc func getData(){
        
    }
}
 
